/*
 * @Description: 
 * @Autor: zhangbing
 * @Date: 2021-07-28 11:48:18
 * @LastEditors: zhangbing
 * @LastEditTime: 2021-07-28 11:57:42
 */

export default function throttle(fn, delay) {
    var t = null,
        begin = new Date().getTime() // 开始的时间戳

    return function () {
        var _self = this // 保存执行上下文
        var args = arguments // 可能会有参数
        var cur = new Date().getTime() // 当前时间戳

        clearTimeout(t)

        if (cur - begin >= delay) {
            fn.apply(_self, args)
            begin = cur
        } else {
            t = setTimeout(function () {
                fn.apply(_self, args)
            }, delay)
        }
    }
}
